#ifndef RESOURCE_CONNECTION_H
#define RESOURCE_CONNECTION_H
#include "async/asocket.h"
#include "parser.h"

class Server;


class Connection : public asocket
{
public:
    class Request {
    public:
        Request(Connection &conn) :read(conn.read), write(conn.write), head(conn.head){ }
        virtual ~Request(){}

        inline const std::string& method()const noexcept { return head.method; }
        inline const std::string& version()const noexcept { return head.version; }
        inline const std::unordered_map<std::string, std::string>& headers()const noexcept { return head.headers; }

        template<typename T>
        T GET(const std::string& key) { throw std::runtime_error("Request GET ERROR"); }

        async_io::_read &read;
        async_io::_write &write;

    protected:
        http_head& head;
    };
    friend class Server;
    friend class Request;
    friend class Response;
    explicit Connection(Server *svr = nullptr);

    //! interface
    async<> handle();

    //! Server require
    void connect();

    void closing()override;

protected:
    void on_close()override;

    int max_header_size = 4096;
    Server *svr;
    std::string head_buffer;
    http_head head;
    Request request;
};

typedef Connection::Request Request;


#endif //RESOURCE_CONNECTION_H
